home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / StreamImageSource.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  4.5 KB  |  133 lines

  1. package symantec.itools.db.awt;
  2.  
  3. import java.io.InputStream;
  4. import java.io.BufferedInputStream;
  5. import java.net.URL;
  6. import java.net.MalformedURLException;
  7.  
  8.  
  9. /**
  10.  * An image producer which can produce the image data for Images from any
  11.  * input stream.  It currently support GIF and JPEG formats.
  12.  * <P>
  13.  * For security reasons, whenever an instance of StreamInputSource is created,
  14.  * a security check is performed against a URL object that must point the
  15.  * machine the input stream is pulling the image data.  If the constructor is
  16.  * called without specifying a base URL, a default base maintained by the class
  17.  * is used for the security check. By default, the base URL is set with a
  18.  * protocol of type file.  If an image is being created within an
  19.  * applet that was delivered over a network, it will be necessary to change the
  20.  * base URL reference to the code base of the applet; otherwise, a security
  21.  * exception will result.
  22.  * <P>
  23.  * Here is an example of how to use the class to create an image within an applet:
  24.  * <pre>
  25.     public class ImageTest extends Applet {
  26.         Image       image;
  27.         StreamImageSource   sis;
  28.  
  29.         public void init() {
  30.             try {
  31.                 StreamImageSource.setBaseUrl(getCodeBase());
  32.  
  33.                 Toolkit             tk = getToolkit();
  34.                 URL                 url = new URL(getDocumentBase(), "test.jpg");
  35.                 InputStream         is = url.openStream();
  36.  
  37.                 //create an image using StreamImageSource and applet's toolkit
  38.                 is = new StreamImageSource(is, StreamImageSource.JPEG);
  39.                 image = tk.createImage(sis);
  40.             } catch(Exception e) { e.printStackTrace(); }
  41.         }
  42.  
  43.         public void paint(Graphics g) {
  44.             g.drawImage(image, 0, 0, this);
  45.         }
  46.     }</pre>
  47.  */
  48. public class StreamImageSource extends sun.awt.image.URLImageSource
  49.                                implements java.awt.image.ImageProducer
  50. {
  51.     InputStream     stream;
  52.     int             type;
  53.     URL             base;
  54.  
  55.     static URL      baseUrl;
  56.  
  57.     /**
  58.      * Constant for GIF format
  59.      */
  60.     public static final int GIF = 0;
  61.     /**
  62.      * Constant for JPEG format
  63.      */
  64.     public static final int JPEG = 1;
  65.  
  66.     static {
  67.         //initialize the base url with the file protocol
  68.         try {
  69.             baseUrl = new URL("file://");
  70.         } catch(MalformedURLException e) {}
  71.     }
  72.  
  73.     /**
  74.      * Create StreamImageSource using the specified stream that will deliver
  75.      * an image of the specified type.  Security check is performed against
  76.      * the base URL.
  77.      * @param is   input stream connected to the data source for the image
  78.      * @param type the type of image (one of GIF or JPEG)
  79.      */
  80.     public StreamImageSource(InputStream is, int type) {
  81.         this(baseUrl , is, type);
  82.     }
  83.  
  84.     /**
  85.      * Create StreamImageSource using the specified stream that will deliver
  86.      * an image of the specified type.  Security check is performed against
  87.      * the URL reference passed to the constructor.
  88.      * @param base url from which the input stream is gathering data
  89.      * @param is   input stream connected to the data source for the image
  90.      * @param type the type of image (one of GIF or JPEG)
  91.      */
  92.     public StreamImageSource(URL base, InputStream is, int type) {
  93.         super(base);
  94.  
  95.         if (type != GIF && type != JPEG) {
  96.             throw new IllegalArgumentException("Unsupported image type:" + type);
  97.         }
  98.  
  99.         stream = is;
  100.         this.type = type;
  101.     }
  102.  
  103.     /**
  104.      * Sets the default URL that security checks are performed.
  105.      */
  106.     public static void setBaseUrl(URL base) { baseUrl = base; }
  107.  
  108.     /**
  109.      * Gets the URL for which secrity checks are performed when an
  110.      * instance of StreamImageSource is created without specifying
  111.      * a base URL.
  112.      */
  113.     public static URL getBaseUrl() { return baseUrl; }
  114.  
  115.     /**
  116.      * Creates the proper decoder based on the type of image specified
  117.      * in the constructor.
  118.      */
  119.     protected sun.awt.image.ImageDecoder getDecoder() {
  120.         InputStream is;
  121.         is = new BufferedInputStream(stream);
  122.  
  123.         switch (type) {
  124.             case GIF:
  125.                 return new sun.awt.image.GifImageDecoder(this, is);
  126.             case JPEG:
  127.                 return new sun.awt.image.JPEGImageDecoder(this, is);
  128.             default:
  129.                 throw new IllegalArgumentException("Unsupported image type:" + type);
  130.         }
  131.     }
  132. }
  133.